<?php

namespace Managers;

class Gallery extends BaseManager
{
    protected $__table = '\Catalog\Gallery';
    protected $__table2 = '\Catalog\Rotator';

    public function getAll($public=1){
         $qb = $this->_em->createQueryBuilder()
                ->select('photos')
                ->from($this->__table, 'photos')
                ->where("photos.ispublish= :public")->setParameter('public', $public);


         $photos = $qb

                ->getQuery()
                ->getArrayResult();
        return $photos;
    }

    public function getList($limit, $start = 0, $public = 1)
    {
        $qb = $this->_em->createQueryBuilder()
                ->select('photos')
                ->from($this->__table, 'photos');
        if (isset($public)) {
            $qb = $qb->where("photos.ispublish= :public")->setParameter('public', $public);
        }

        $photos = $qb
                ->setFirstResult($start)
                ->setMaxResults($limit)
                ->getQuery()
                ->getArrayResult();
        return $photos;
    }

    public  function  getOne($id)
    {
       $res = $this->_em->createQueryBuilder()
                ->select('ph')
                ->from($this->__table, 'ph')
                ->where("ph.id= :id")->setParameter('id', $id)
                ->getQuery()
                ->getArrayResult();
        return $res;
    }

    public function getTotalRecords()
    {
        $res = $this->_em->createQueryBuilder()
                ->select('count (ph.id) as total')
                ->from($this->__table, 'ph')
                ->getQuery()
                ->getArrayResult();

        return $res[0]['total'];
    }

    function resizer($name, $des_a_img_url, $file, $d_x)
    {
        if (!extension_loaded('imagick')) {
            throw new \Exception('Image magick php is not installed.');
        }
        try {
            $image = new \Imagick($file);
            $image->thumbnailImage($d_x, null);
            $fd = fopen($des_a_img_url, 'w');
            $image->writeImageFile($fd);
            fclose($fd);
            unlink($file);
        } catch (\ImagickException $e) {
            throw new \Exception('Unsupported file format.');
        }
    }



    public function add($ispublish)
    {
        $object = new \Catalog\Gallery();
        $object->setIspublish($ispublish);
        $this->_em->persist($object);
        $this->_em->flush();

        return $object;
    }
//
    function setPosted($id)
    {
        $object = $this->_em->find($this->__table, $id);
        if ($object) {
            $object->setIspublish(1);

            $this->_em->persist($object);
            $this->_em->flush();
            return true;
        }
        return false;
    }

    function setImage($id, $img) {
        $object = $this->_em->find($this->__table, $id);
        if ($object) {
            $object->setSrc($img);

            $this->_em->persist($object);
            $this->_em->flush();
            return $object;
        }
        return false;
    }

    public function pictureRotator($ispublish)
      {
          $object = new \Catalog\Rotator();
          $object->setIspublished($ispublish);
          $object->setImg("");
          $object->setDescript("");
          $this->_em->persist($object);
          $this->_em->flush();

          return $object;
      }
    function setImageRotator($id, $img) {
//        print '<pre>';
//        print_r(get_defined_vars());
//        print '</pre>';

        $object = $this->_em->find($this->__table2, $id);
//        print '<pre>';
//        print_r(\Doctrine\Common\Util\Debug::dump($object));
//        print '</pre>';
//        die;
        if ($object) {

            $object->setImg($img);
            $this->_em->persist($object);
            $this->_em->flush();
            return $object;
        }
        return false;
    }
    public function savePublic($id,$img,$public )
       {


           $page = $this->_em->find($this->__table2, $id);

           if($page) {
           $page->setImg($img);
           $page->setIspublished($public);
           $this->_em->persist($page);
           $this->_em->flush();
           return $page;
           }
           return false;
       }

}